﻿package mortal.game.scene3D.util
{
    import Message.Public.*;
    import extend.language.*;
    import mortal.game.manager.*;
    import mortal.game.scene3D.ai.ais.*;
    import mortal.game.scene3D.ai.data.*;
    import mortal.game.scene3D.layer3D.*;
    import mortal.game.scene3D.layer3D.utils.*;
    import mortal.game.scene3D.map3D.*;
    import mortal.game.scene3D.player.entity.*;
    import mortal.game.utils.*;
    import mortal.game.view.skill.*;
    import mortal.game.view.systemSetting.*;

    public class FightUtil extends Object
    {

        public function FightUtil()
        {
            return;
        }// end function

        public static function isNormalAttackable(param1:IEntity, param2:Boolean = true) : Boolean
        {
            return EntityDictionary.isEntityFit(param1, null, EntityRelationUtil.ENEMY, false, true, param2);
        }// end function

        public static function selectEntityBySkill(param1:FollowFightAIData, param2:Boolean = false, param3:Boolean = false) : void
        {
            var _loc_4:SkillInfo = null;
            var _loc_6:IEntity = null;
            var _loc_10:Boolean = false;
            var _loc_13:Boolean = false;
            var _loc_14:Boolean = false;
            _loc_4 = param1.skillInfo;
            param1.range = _loc_4.distance;
            param1.isSkillThenWalk = false;
            param1.mouseFontEntity = null;
            var _loc_5:* = ThingUtil.entityUtil.entitysMap.getEntityByRangle(SceneRange.display);
            var _loc_7:* = _loc_4.tSkill.targetType;
            var _loc_8:* = _loc_4.tSkill.type == ESkillType._ESkillTypeTransfer;
            if (_loc_8)
            {
                setDirectionParams(param1, param2);
                return;
            }
            var _loc_9:* = Math.pow(2, 30);
            var _loc_11:int = 0;
            var _loc_12:int = 1;
            while (_loc_12 < _loc_9)
            {
                
                if ((_loc_7 & _loc_12) > 0)
                {
                    if (_loc_12 == ESkillTargetType._ESkillTargetTypeEnemy)
                    {
                        _loc_13 = false;
                        if (ThingUtil.selectEntity != null)
                        {
                            _loc_13 = EntityDictionary.isEntityFit(ThingUtil.selectEntity, null, EntityRelationUtil.ENEMY, false, true);
                        }
                        switch(_loc_4.tSkill.targetSelect)
                        {
                            case ESkillTargetSelect._ESkillTargetSelectNeedTarget:
                            {
                                if (ThingUtil.selectEntity != null)
                                {
                                    param1.entitys = [ThingUtil.selectEntity.entityInfo.entityInfo.entityId];
                                    _loc_11 = ThingUtil.selectEntity.entityInfo.bodySize;
                                }
                                if (_loc_13)
                                {
                                    param1.target = ThingUtil.selectEntity;
                                    param1.range = RolePlayer.instance.entityInfo.bodySize + _loc_4.distance + _loc_11;
                                }
                                return;
                            }
                            case ESkillTargetSelect._ESkillTargetSelectAutoTarget:
                            {
                                _loc_6 = getEntityByParams(_loc_4, _loc_5);
                                break;
                            }
                            case ESkillTargetSelect._ESkillTargetSelectSelf:
                            {
                                param1.entitys = [];
                                if (AutoFightAI.isWorking && (_loc_4.tSkill.type == ESkillType._ESKillTypeTornado || _loc_4.tSkill.name == Language.getString(21249)))
                                {
                                    param1.target = ThingUtil.selectEntity;
                                    if (_loc_4.tSkill.type == ESkillType._ESKillTypeTornado)
                                    {
                                        param1.range = Math.max(ThingUtil.selectEntity.entityInfo.bodySize, 100);
                                    }
                                    else
                                    {
                                        param1.range = 300;
                                    }
                                }
                                else
                                {
                                    param1.target = RolePlayer.instance;
                                }
                                return;
                            }
                            case ESkillTargetSelect._ESkillTargetSelectMouseDirection:
                            case ESkillTargetSelect._ESkillTargetSelectMouse:
                            case ESkillTargetSelect._ESkillTargetSelectMouseFront:
                            {
                                param1.entitys = [];
                                _loc_14 = false;
                                _loc_10 = !ClientSetting.local.getIsDone(IsDoneType.SkillAutoSelectStart + _loc_4.position - 1);
                                if (param2)
                                {
                                    if (ThingUtil.selectEntity == null)
                                    {
                                        param1.entitys = null;
                                        param1.point = null;
                                    }
                                    else if (_loc_13)
                                    {
                                        param1.entitys = [ThingUtil.selectEntity.entityInfo.entityInfo.entityId];
                                        param1.isSkillThenWalk = false;
                                        param1.point = getSPoint(ThingUtil.selectEntity);
                                        param1.mouseFontEntity = ThingUtil.selectEntity;
                                    }
                                }
                                else if (_loc_10)
                                {
                                    if (ThingUtil.selectEntity == null)
                                    {
                                        param1.entitys = [];
                                        param1.point = SceneRange.getMousePoint();
                                        if (_loc_4.tSkill.targetSelect != ESkillTargetSelect._ESkillTargetSelectMouseFront)
                                        {
                                            param1.isSkillThenWalk = true;
                                        }
                                    }
                                    else if (_loc_13)
                                    {
                                        param1.entitys = [ThingUtil.selectEntity.entityInfo.entityInfo.entityId];
                                        param1.point = getSPoint(ThingUtil.selectEntity);
                                        param1.mouseFontEntity = ThingUtil.selectEntity;
                                    }
                                }
                                else if (AutoFightAI.isWorking)
                                {
                                    param1.entitys = [ThingUtil.selectEntity.entityInfo.entityInfo.entityId];
                                    param1.point = getSPoint(ThingUtil.selectEntity);
                                    param1.mouseFontEntity = ThingUtil.selectEntity;
                                }
                                else
                                {
                                    param1.entitys = [];
                                    param1.point = SceneRange.getMousePoint();
                                    if (_loc_4.tSkill.targetSelect != ESkillTargetSelect._ESkillTargetSelectMouseFront)
                                    {
                                        param1.isSkillThenWalk = true;
                                    }
                                    _loc_14 = true;
                                }
                                if (param1.point != null && _loc_4.tSkill.targetSelect == ESkillTargetSelect._ESkillTargetSelectMouse)
                                {
                                    param1.range = _loc_4.distance + RolePlayer.instance.entityInfo.bodySize;
                                }
                                else if (_loc_4.tSkill.targetSelect == ESkillTargetSelect._ESkillTargetSelectMouseDirection)
                                {
                                    if (ThingUtil.selectEntity != null && _loc_10)
                                    {
                                        param1.range = _loc_4.distance + RolePlayer.instance.entityInfo.bodySize;
                                        param1.isSkillThenWalk = false;
                                    }
                                    else
                                    {
                                        param1.range = 1000000;
                                        param1.isSkillThenWalk = true;
                                    }
                                }
                                if (_loc_13)
                                {
                                    param1.target = ThingUtil.selectEntity;
                                    if (_loc_14)
                                    {
                                        param1.target = null;
                                    }
                                }
                                if (_loc_4.tSkill.targetSelect == ESkillTargetSelect._ESkillTargetSelectMouse)
                                {
                                    param1.isSkillThenWalk = false;
                                }
                                return;
                            }
                            default:
                            {
                                break;
                            }
                        }
                    }
                    else
                    {
                        _loc_6 = getEntityByParams(_loc_4, _loc_5);
                    }
                    if (_loc_6 != null)
                    {
                        param1.target = _loc_6;
                        param1.range = _loc_4.distance + RolePlayer.instance.entityInfo.bodySize + _loc_6.entityInfo.bodySize;
                        param1.entitys = [_loc_6.entityInfo.entityInfo.entityId];
                        return;
                    }
                }
                _loc_12 = _loc_12 * 2;
            }
            return;
        }// end function

        private static function setDirectionParams(param1:FollowFightAIData, param2:Boolean = false) : void
        {
            var _loc_3:Number = NaN;
            param1.entitys = [];
            if (param2)
            {
                _loc_3 = RolePlayer.instance.direction / 180 * Math.PI;
                param1.point = new SPoint();
                param1.point.x = RolePlayer.instance.x2d + 100 * Math.cos(_loc_3);
                param1.point.y = RolePlayer.instance.y2d + 100 * Math.sin(_loc_3);
            }
            else
            {
                param1.point = SceneRange.getMousePoint();
                param1.isSkillThenWalk = true;
            }
            return;
        }// end function

        public static function getSPoint(param1:IEntity) : SPoint
        {
            var _loc_2:* = new SPoint();
            if (param1 is SpritePlayer)
            {
                _loc_2.x = (param1 as SpritePlayer).realX2d;
                _loc_2.y = (param1 as SpritePlayer).realY2d;
            }
            else
            {
                _loc_2.x = param1.x2d;
                _loc_2.y = param1.y2d;
            }
            return _loc_2;
        }// end function

        public static function getTargetEntityTypes(param1:int) : Array
        {
            var _loc_2:Array = [];
            if (param1 == ESkillTargetEntityType._ESkillTargetEntityTypeAll)
            {
                _loc_2[EEntityType._EEntityTypeBoss] = true;
                _loc_2[EEntityType._EEntityTypePet] = true;
                _loc_2[EEntityType._EEntityTypePlayer] = true;
            }
            else
            {
                if (param1 & EEntityType._EEntityTypeBoss)
                {
                    _loc_2[EEntityType._EEntityTypeBoss] = true;
                }
                if (param1 & EEntityType._EEntityTypePet)
                {
                    _loc_2[EEntityType._EEntityTypePet] = true;
                }
                if (param1 & EEntityType._EEntityTypePlayer)
                {
                    _loc_2[EEntityType._EEntityTypePlayer] = true;
                }
            }
            return _loc_2;
        }// end function

        private static function getEntityByParams(param1:SkillInfo, param2:Array) : IEntity
        {
            var _loc_4:int = 0;
            if (_loc_4 == ESkillTargetType._ESkillTargetTypeSelf || _loc_4 == ESkillTargetType._ESkillTargetTypeMaster)
            {
                return RolePlayer.instance;
            }
            var _loc_3:* = EntityRelationUtil.ENEMY;
            _loc_4 = param1.tSkill.targetType;
            var _loc_5:* = getTargetEntityTypes(param1.tSkill.targetEntityType);
            if (_loc_4 & ESkillTargetType._ESkillTargetTypeFriend || _loc_4 & ESkillTargetType._ESkillTargetTypeSelf || _loc_4 & ESkillTargetType._ESkillTargetTypeMaster)
            {
                _loc_3 = EntityRelationUtil.FIREND;
            }
            else if (_loc_4 & ESkillTargetType._ESkillTargetTypeEnemy)
            {
                _loc_3 = EntityRelationUtil.ENEMY;
            }
            var _loc_6:* = (param1.tSkill.targetEntityType & ESkillTargetEntityType._ESkillTargetEntityTypeDeadBody) > 0;
            return ThingUtil.entityUtil.entitysMap.selectEntity(_loc_5, _loc_3, _loc_6, param2);
        }// end function

        public static function isCanAttackPlayer(param1:Boolean = false) : Boolean
        {
            if (RolePlayer.instance.isInSwinArea)
            {
                if (param1)
                {
                    MsgManager.showRollTipsMsg(Language.getString(20679));
                }
                return false;
            }
            return true;
        }// end function

    }
}
